
package com.tools.cleanmaster.store;

import android.content.Context;
import android.content.SharedPreferences;

import com.tools.cleanmaster.CleanApplication;

public class TrashCloudSharedPrefManager {

    private static final String PREFS_FILE = "trash_cloud";

    private static final String PREFS_KEY_LAST_TIME = "last_time";

    private static final String PREFS_KEY_HAS_PULL_DAY = "has_pull_day";

    private static final String PREFS_KEY_PKG_NUM = "pkg_num";

    private static final String PREFS_KEY_LAST_FAILED_TIME = "last_failed_time";

    /**
     * 更新次数
     */
    private static final String PREFS_KEY_UPDATEKEYS = "update_t";

    /**
     * 更新时间
     */
    private static final String PREFS_KEY_UPDATETIME = "update_time";

    /**
     * 记录上次云端路径更新到的ID号
     */
    private static final String PREFS_KEY_CLOUD_UPDATE_ID = "cloud_id";

    private static final String PREFS_KEY_UNKONWN_CLEAN = "un_clean";

    /**
     * 是否云扫过
     */
    private static final String PREFS_KEY_ALREADY_CLOUDY_SCAN = "already_cloudy_scan";

    /**
     * 后台扫描间隔时间
     */
    private static final String PREFS_KEY_BACK_SCAN_TIME = "back_scan_time";

    private static SharedPreferences sPrefs = null;

    private static Context sContext = null;

    private TrashCloudSharedPrefManager() {
    }

    public static SharedPreferences initSharedPreferences() {
        if (sPrefs == null) {
            sContext = CleanApplication.getInstance();
            sPrefs = sContext.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
        }
        return sPrefs;
    }

    public static long getLastUpdateTime() {
        initSharedPreferences();
        return sPrefs.getLong(PREFS_KEY_LAST_TIME, 0);
    }

    public static void setLastUpdateTime(long time) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_LAST_TIME, time));
    }

    public static boolean getHasPullInOneDay() {
        initSharedPreferences();
        return sPrefs.getBoolean(PREFS_KEY_HAS_PULL_DAY, false);
    }

    public static void setHasPullInOneDay(boolean hasPull) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putBoolean(PREFS_KEY_HAS_PULL_DAY, hasPull));
    }

    public static int getChangedPkgNum() {
        initSharedPreferences();
        return sPrefs.getInt(PREFS_KEY_PKG_NUM, 0);
    }

    public static void clearChangedPkgNum() {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putInt(PREFS_KEY_PKG_NUM, 0));
    }

    public static void addChangedPkgNum() {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putInt(PREFS_KEY_PKG_NUM,
                getChangedPkgNum() + 1));
    }

    public static long getLastFailedTime() {
        initSharedPreferences();
        return sPrefs.getLong(PREFS_KEY_LAST_FAILED_TIME, 0);
    }

    public static void setLastFailedTime(long time) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_LAST_FAILED_TIME, time));
    }

    /**
     * @return 返回更新次数，每次更新加一
     */
    public static int getUpdateTimes() {
        initSharedPreferences();
        return sPrefs.getInt(PREFS_KEY_UPDATEKEYS, 1);
    }

    public static void setUpdateTimes() {
        initSharedPreferences();
        sPrefs.edit().putInt(PREFS_KEY_UPDATEKEYS, sPrefs.getInt(PREFS_KEY_UPDATEKEYS, 1) + 1).apply();
    }

    public static void resetUpdateTimes() {
        initSharedPreferences();
        sPrefs.edit().putInt(PREFS_KEY_UPDATEKEYS, 1).apply();
    }

    /**
     * @return 获得上次更新时间，这个是用于数据库和云端路径更新的，不用于未知路径
     */
    public static long getUpdateTime() {
        initSharedPreferences();
        return sPrefs.getLong(PREFS_KEY_UPDATETIME, 0);
    }

    /**
     * 设置上次更新时间,这个是用于数据库和云端路径更新的，不用于未知路径
     */
    public static void setUpdateTime(long updateTime) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_UPDATETIME, updateTime));
    }

    public static void setCloudLastUpdateId(long id) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_CLOUD_UPDATE_ID, id));
    }

    public static long getCloudLastCloudId() {
        initSharedPreferences();
        return sPrefs.getLong(PREFS_KEY_CLOUD_UPDATE_ID, 0);
    }

    /**
     * 设置unknow库更新时间，即清空时间
     */
    public static void setUnkonwnClean(long time) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_UNKONWN_CLEAN, time));
    }

    /**
     * 获取上次unKnow库的更新时间，即清空时间
     */
    public static long getUnkonwnCleanTime() {
        initSharedPreferences();
        long firstTime = sPrefs.getLong(PREFS_KEY_UNKONWN_CLEAN, 0);
        return firstTime;
    }

    public static void setCloudyScanReport(boolean result) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putBoolean(PREFS_KEY_ALREADY_CLOUDY_SCAN, result));
    }

    /**
     * 是否已经云扫,设置已经扫描过
     */
    public static boolean isCloudyScanedReport() {
        initSharedPreferences();
        boolean scanned = sPrefs.getBoolean(PREFS_KEY_ALREADY_CLOUDY_SCAN, false);
        return scanned;
    }

    /**
     * 后台扫描时间
     */
    public static void setBackScannerTime(long time) {
        initSharedPreferences();
        SharedPreferencesCompat.apply(sPrefs.edit().putLong(PREFS_KEY_BACK_SCAN_TIME, time));
    }

    /**
     * 获取上次后台扫描时间
     *
     * @return
     */
    public static long getBackScannerTime() {
        initSharedPreferences();
        return sPrefs.getLong(PREFS_KEY_BACK_SCAN_TIME, 0);
    }
}
